package share.servlet;

import java.io.IOException;
import java.net.URLDecoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import share.storage.DirectoryTree;
import share.storage.io.File;

public class DirBrowserServlet extends HttpServlet {

	private static final long serialVersionUID = -4267751669194217466L;

	@Override
	protected long getLastModified(HttpServletRequest req) {
		return super.getLastModified(req);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String path = request.getRequestURI().replace(
				getServletContext().getContextPath() + "/files", "");
		// 获取文件路径
		path = URLDecoder.decode(path, "UTF-8");
		DirectoryTree dt = DirectoryTree.getInstance();
		File file = dt.findByPath(path);
		if (file == null) {
			response.sendError(404);
			return;
		}
		request.setAttribute("file", file);
		if (file.isDirectory()) {
			request.getRequestDispatcher("/dirBrowser.jsp").forward(request,
					response);
		} else {
			request.getRequestDispatcher("/fileDetail.jsp").forward(request,
					response);
		}
	}

}
